package com.tl.boot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tl.boot.common.annotation.WithPermission;
import com.tl.boot.common.vo.PageInfoVO;
import com.tl.boot.common.vo.ResponseVO;
import com.tl.boot.entity.user.dto.AddUserInfoDTO;
import com.tl.boot.entity.user.dto.ListUserDTO;
import com.tl.boot.entity.user.dto.UpdateUserDTO;
import com.tl.boot.entity.user.form.*;
import com.tl.boot.entity.user.vo.UserInfoVO;
import com.tl.boot.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * com.tl.boot.controller -- UserController
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/16 11:07
 */
@RestController
@RequestMapping("auth/user")
@Api(tags = "用户管理模块")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("add")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    @ApiOperation(value = "添加用户基本信息", notes = "传添加用户基本信息请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addUserInfoForm", value = "添加用户基本信息请求对象", required = true,
                    dataType = "AddUserInfoForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO addUserBasicInfo(@RequestBody AddUserInfoForm addUserInfoForm) {
        AddUserInfoDTO dto = new AddUserInfoDTO();
        BeanUtils.copyProperties(addUserInfoForm,dto);
        dto.setUserStatus(addUserInfoForm.getUserStatusEnum().getCode().shortValue());
        Long userId = userService.addUserBasicInfo(dto);
        return ResponseVO.buildSuccess(userId);
    }

    @PostMapping("delete")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    @ApiOperation(value = "删除用户信息", notes = "传删除用户信息请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "delUserForm", value = "删除用户信息请求对象", required = true,
                    dataType = "DelUserForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO delUserInfo(@RequestBody DelUserForm delUserForm) {
        return ResponseVO.buildSuccess();
    }

    @PostMapping("update")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    @ApiOperation(value = "更新用户信息", notes = "传更新用户信息请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updateUserForm", value = "更新用户信息请求对象", required = true,
                    dataType = "UpdateUserForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO updateUserInfo(@RequestBody UpdateUserForm updateUserForm) {
        UpdateUserDTO dto = new UpdateUserDTO();
        BeanUtils.copyProperties(updateUserForm,dto);
        dto.setUserStatus(updateUserForm.getUserStatusEnum().getCode().shortValue());
        userService.updateUserBasicInfo(dto);
        return ResponseVO.buildSuccess();
    }

    @PostMapping("query")
    @ApiOperation(value = "查询用户信息", notes = "传查询用户信息请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "form", value = "查询用户信息请求对象", required = true,
                    dataType = "QueryUserInfoForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO queryUserDetailInfo(@RequestBody QueryUserInfoForm form) {

        return ResponseVO.buildSuccess();
    }

    @PostMapping("list")
    @ApiOperation(value = "用户信息列表", notes = "传用户信息列表请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "listUserInfoForm", value = "用户信息列表请求对象", required = true,
                    dataType = "ListUserInfoForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO<PageInfoVO<UserInfoVO>> listUserInfo(@RequestBody ListUserInfoForm listUserInfoForm) {
        ListUserDTO dto = new ListUserDTO();
        BeanUtils.copyProperties(listUserInfoForm,dto);
        dto.setGender(listUserInfoForm.getGenderEnum() == null?
                null:listUserInfoForm.getGenderEnum().getCode().shortValue());
        PageHelper.startPage(listUserInfoForm.getPage(), listUserInfoForm.getSize());
        List<UserInfoVO> userInfoVOS = userService.listUsers(dto);
        Page<UserInfoVO> page = (Page<UserInfoVO>) userInfoVOS;
        return ResponseVO.buildSuccess(page.getResult(),page.getTotal());
    }
}
